package com.epfresh.api.global;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.supplier.entity.StoreInfo;
import com.epfresh.api.entity.User;


public abstract class BaseFragment extends Fragment implements View.OnClickListener {

    public BaseApplication getAppApplication() {
        if (getActivity() == null) {
            return null;
        }
        return getAppActivity().getAppApplication();
    }

    public BaseActivity getAppActivity() {
        FragmentActivity activity = getActivity();
        return activity != null ? (BaseActivity) activity : null;
    }

    public User getUser() {
        if (getActivity() == null) {
            return null;
        }
        return getAppActivity().getUser();
    }

    public StoreInfo getStoreInfo() {
        if (getActivity() == null) {
            return null;
        }
        return getAppActivity().getStoreInfo();
    }

    public void callBack() {

    }

    public abstract String getBaseTag();

    public void updateUser(User user) {
        if (getAppActivity() != null)
            getAppActivity().updateUser(user);
    }

    public void updateStoreInfo(StoreInfo storeInfo) {
        if (getAppActivity() != null)
            getAppActivity().updateStoreInfo(storeInfo);
    }

    //request不能为null
    protected <T> void request(Object data, Object tag, OnRequestListener<T> onRequestListener) {

        if (getAppActivity() == null && getUser() == null) {
            return;
        }
        getAppActivity().request(data, tag, onRequestListener);

    }

    /**
     * 这是为我要买模块添加的
     * @param data
     * @param tag
     * @param onRequestListener
     * @param <T>
     */
    protected <T> void requestBuyer(Object data, Object tag, OnRequestListener<T> onRequestListener) {

        if (getAppActivity() == null && getUser() == null) {
            return;
        }
        getAppActivity().requestBuyer(data, tag, onRequestListener);
    }
    public void showProgressDialog() {
        if (getAppActivity() == null) {
            return;
        }
        getAppActivity().showProgressDialog();
    }

    public void hideProgressDialog() {
        if (getAppActivity() == null) {
            return;
        }
        getAppActivity().hideProgressDialog();
    }


    @Override
    public void onClick(View v) {

    }
}
